package com.chuanyang.driverh.amap;

import android.content.Context;
import android.text.TextUtils;
import android.util.Log;

import com.amap.api.maps.model.LatLng;
import com.amap.api.services.core.LatLonPoint;
import com.amap.api.services.core.PoiItem;
import com.amap.api.services.geocoder.GeocodeResult;
import com.amap.api.services.geocoder.GeocodeSearch;
import com.amap.api.services.geocoder.GeocodeSearch.OnGeocodeSearchListener;
import com.amap.api.services.geocoder.RegeocodeAddress;
import com.amap.api.services.geocoder.RegeocodeQuery;
import com.amap.api.services.geocoder.RegeocodeResult;

import java.util.List;

/**
 * 封装高德地图的反编译的api功能
 * 用法:
 *  1.new GeoCodeSearchUtil(Context);
 *  2.initGeoCodeSearch();
 *  3.发起反编译:startRecoderSearch
 *  4.编译回调:setOnRegecodeSuccessListener();   
 *           setOnRegeocodeDetailSuccessListener()
 *  
 *  
 */
public class AMapRegeocodeSearchUtil implements OnGeocodeSearchListener {

	private Context context;

	private GeocodeSearch geocoderSearch;
	private RegeocodeQuery query;
	private LatLonPoint latLonPoint;

	private LocationEntity locationEntity; //存储反编译获得的数据


	public AMapRegeocodeSearchUtil(Context context) {
		this.context=context;
		latLonPoint=new LatLonPoint(0, 0);
		locationEntity=new LocationEntity();
	}


	/**
	 * 初始化反编译相关的类
	 */
	public void initGeoCodeSearch(){
		geocoderSearch = new GeocodeSearch(context);     //地理编码功能类
		//反编码查询对象:latLonPoint参数表示一个Latlng，第二参数表示范围多少米，GeocodeSearch.AMAP表示是国测局坐标系还是GPS原生坐标系
		query = new RegeocodeQuery(null, 200, GeocodeSearch.AMAP);
		geocoderSearch.setOnGeocodeSearchListener(this); //编码事件监听
	}

	/**
	 * 开启反编译
	 * @param location
	 */
	public void startRecoderSearch(LatLng location){
		//经纬度
		locationEntity.setLatitude(location.latitude);
		locationEntity.setLongitude(location.longitude);
		//
		latLonPoint.setLatitude(location.latitude);
		latLonPoint.setLongitude(location.longitude);
		query.setPoint(latLonPoint);
		geocoderSearch.getFromLocationAsyn(query);// 设置同步逆地理编码请求

	}

	@Override
	public void onRegeocodeSearched(RegeocodeResult result, int code) {
		if(code==1000&&result != null && result.getRegeocodeAddress() != null){
			RegeocodeAddress regeocodeAddr = result.getRegeocodeAddress();
			//反编译获取的省市区 地址 建筑物
			String province=regeocodeAddr.getProvince();
			String city=regeocodeAddr.getCity();
			String district=regeocodeAddr.getDistrict();
			String address=regeocodeAddr.getFormatAddress();
			String building=regeocodeAddr.getBuilding();

			locationEntity.setProvince(province);
			locationEntity.setCity(TextUtils.isEmpty(city)?province:city); //直辖市的city为空 province即为城市名
			locationEntity.setDistrict(district);
			locationEntity.setAddress(address);
			if(TextUtils.isEmpty(building)&&regeocodeAddr.getPois()!=null&&regeocodeAddr.getPois().size()>0){
				building=regeocodeAddr.getPois().get(0).getTitle();
				if(onRegeocodePoiListSuccessListener!=null){
					onRegeocodePoiListSuccessListener.onRegeocodePoiListSuccess
							(regeocodeAddr.getPois(),province,city,district);
				}
			}
			locationEntity.setBuilding(building);

			if(onRegecodeSuccessListener!=null){
				onRegecodeSuccessListener.onRegecodeSuccess(building, address);
			}
			if(detailSuccessListener!=null){
				detailSuccessListener.onRegeocodeDetailSuccess(locationEntity);
			}

			if(regeocodeAddr.getPois()!=null&&regeocodeAddr.getPois().size()>0){
				if(onRegeocodePoiListSuccessListener!=null){
					onRegeocodePoiListSuccessListener.onRegeocodePoiListSuccess(regeocodeAddr.getPois(),province,city,district);
				}
			}
		}

	}

	@Override
	public void onGeocodeSearched(GeocodeResult result, int code) {
		Log.d("mylocAddr",result.getGeocodeAddressList().get(1).getBuilding());
	}

	//反编译为了获取building address
	public interface OnRegecodeSuccessListener{
		void onRegecodeSuccess(String building, String address);
	}
	private OnRegecodeSuccessListener onRegecodeSuccessListener;
	public void setOnRegecodeSuccessListener(OnRegecodeSuccessListener onRegecodeSuccessListener){
		this.onRegecodeSuccessListener=onRegecodeSuccessListener;
	}

	//反编译为了获取位置数据
	public interface onRegeocodeDetailSuccessListener{
		void onRegeocodeDetailSuccess(LocationEntity loc);
	}
	private onRegeocodeDetailSuccessListener detailSuccessListener;
	public void setOnRegeocodeDetailSuccessListener(onRegeocodeDetailSuccessListener detailSuccessListener){
		this.detailSuccessListener=detailSuccessListener;
	}

	////反编译为了获取对应经纬度附件的地址列表---地图选点
	public interface OnRegeocodePoiListSuccessListener{
		void onRegeocodePoiListSuccess(List<PoiItem> items, String pro, String city, String region);
	}
	private OnRegeocodePoiListSuccessListener onRegeocodePoiListSuccessListener;
	public void setOnRegeocodePoiListSuccessListener(OnRegeocodePoiListSuccessListener onRegeocodePoiListSuccessListener){
		this.onRegeocodePoiListSuccessListener=onRegeocodePoiListSuccessListener;
	}

}
